"""
@File    :   router.py
@Time    :   2024/02/18 16:29:45
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   用户路由
"""

from typing import Annotated

from fastapi import APIRouter, Depends

from src.business.admin.system.oauth2.token.dependencies.no_introduction import (
    DependVerifyAccessTokenMenumPermission,
)
from src.enums.permission_operation import PermissionOperationEnum
from src.utils.unified_response import SuccessApiJSONResponse

from .dependencies.route_depend import (
    route_depend_assign_user_role,
    route_depend_create_user,
    route_depend_delete_user,
    route_depend_get_self_and_menu_info,
    route_depend_get_self_info,
    route_depend_get_user,
    route_depend_list_user,
    route_depend_reset_user_password,
    route_depend_update_self_avatar,
    route_depend_update_self_info,
    route_depend_update_self_password,
    route_depend_update_user,
    route_depend_update_user_status,
)
from .schemas.out import UserCompleteOutSchema, UserSelfAndMenuOutSchema

# 构建权限验证相关的依赖项
# 查
depend_select_permission_verify = DependVerifyAccessTokenMenumPermission(
    __file__, PermissionOperationEnum.select.value
)
# 增
depend_create_permission_verify = DependVerifyAccessTokenMenumPermission(
    __file__, PermissionOperationEnum.create.value
)
# 改
depend_update_permission_verify = DependVerifyAccessTokenMenumPermission(
    __file__, PermissionOperationEnum.update.value
)
# 删
depend_delete_permission_verify = DependVerifyAccessTokenMenumPermission(
    __file__, PermissionOperationEnum.delete.value
)
# 重置用户密码
depend_reset_user_password_permission_verify = DependVerifyAccessTokenMenumPermission(
    __file__, PermissionOperationEnum.reset_user_password.value
)
# 分配后台用户角色
depend_assign_user_role_permission_verify = DependVerifyAccessTokenMenumPermission(
    __file__, PermissionOperationEnum.assign_user_role.value
)

# 创建路由
router = APIRouter(
    prefix="/admin/system/user",
    tags=[
        "后台用户接口",
    ],
)


@router.get("/get", summary="查询单个后台用户")
async def route_get_user(
    verify_menu_permission: Annotated[None, Depends(depend_select_permission_verify)],
    user: Annotated[UserCompleteOutSchema, Depends(route_depend_get_user)],
):
    return SuccessApiJSONResponse(data=user)


@router.get("/get-self-and-menu-info", summary="获取用户自身及其关联菜单的数据")
async def route_get_self_and_menu_info(
    user_and_menu_info: Annotated[
        UserSelfAndMenuOutSchema, Depends(route_depend_get_self_and_menu_info)
    ],
):
    return SuccessApiJSONResponse(data=user_and_menu_info)


@router.post("/list", summary="查询后台用户列表")
async def route_list_user(
    verify_menu_permission: Annotated[None, Depends(depend_select_permission_verify)],
    list_result: Annotated[
        tuple[list[UserCompleteOutSchema], int], Depends(route_depend_list_user)
    ],
):
    search_pagination_result, search_total = list_result
    data = {"result": search_pagination_result, "total": search_total}
    return SuccessApiJSONResponse(data=data)


@router.post("/create", summary="创建后台用户")
async def route_create_user(
    # 验证用户权限(本菜单权限标识为: admin@system@user:create)
    verify_menu_permission: Annotated[None, Depends(depend_create_permission_verify)],
    created_id: Annotated[int, Depends(route_depend_create_user)],
):
    data = {"created_id": created_id}
    return SuccessApiJSONResponse(message="后台用户创建成功", data=data)


@router.post("/update", summary="修改后台用户")
async def route_update_user(
    verify_menu_permission: Annotated[None, Depends(depend_update_permission_verify)],
    update_operation: Annotated[None, Depends(route_depend_update_user)],
):
    return SuccessApiJSONResponse(message="修改后台用户成功")


@router.post("/update-status", summary="修改后台用户状态")
async def route_update_user_status(
    verify_menu_permission: Annotated[None, Depends(depend_update_permission_verify)],
    update_status_operation: Annotated[None, Depends(route_depend_update_user_status)],
):
    return SuccessApiJSONResponse(message="修改后台用户状态成功")


@router.get("/get-self-info", summary="查询用户自身信息")
async def route_get_self_info(
    user: Annotated[UserCompleteOutSchema, Depends(route_depend_get_self_info)],
):
    return SuccessApiJSONResponse(data=user)


@router.post("/update-self-info", summary="用户修改自身信息")
async def route_update_self_info(
    update_self_operation: Annotated[None, Depends(route_depend_update_self_info)],
):
    return SuccessApiJSONResponse()


@router.post("/update-self-avatar", summary="用户修改自身头像")
async def route_update_self_avatar(
    avatar_rel_path: Annotated[None, Depends(route_depend_update_self_avatar)],
):
    return SuccessApiJSONResponse(data=avatar_rel_path)


@router.post("/update-self-password", summary="用户修改自身密码")
async def route_update_self_password(
    update_self_operation: Annotated[None, Depends(route_depend_update_self_password)],
):
    return SuccessApiJSONResponse()


@router.post("/delete", summary="删除后台用户")
async def route_delete_user(
    verify_menu_permission: Annotated[None, Depends(depend_delete_permission_verify)],
    delete_operation: Annotated[None, Depends(route_depend_delete_user)],
):
    return SuccessApiJSONResponse(message="删除后台用户成功")


@router.post("/reset-password", summary="重置后台用户密码")
async def route_reset_user_password(
    verify_menu_permission: Annotated[
        None, Depends(depend_reset_user_password_permission_verify)
    ],
    reset_operation: Annotated[None, Depends(route_depend_reset_user_password)],
):
    return SuccessApiJSONResponse(message="重置后台用户密码成功")


@router.post("/assign-role", summary="分配后台用户角色")
async def route_assign_user_role(
    verify_menu_permission: Annotated[
        None, Depends(depend_assign_user_role_permission_verify)
    ],
    assign_operation: Annotated[None, Depends(route_depend_assign_user_role)],
):
    return SuccessApiJSONResponse(message="分配后台用户角色成功")
